import pymongo
import MySQLdb

remote='mongodb://192.168.10.5:27017/'
exchanges={
	'SH':1,
	'SZ':2
}
def copy_stk(contract,mysql):
	res=contract['STK'].find({})
	cur=mysql.cursor()
	tpl="insert into exchange_stk (`symbol`, `name`, `order_min`, `precision`, `company_id`, `exchange_id`) VALUES (%s,%s,%s,%s,%s,%s)"

	for row in res:
		cur.execute(tpl,(row['symbol'],row['name'],row['trade_min'],row['precision'],1,exchanges[row['exchange']]))
	
	cur.close()
	conn.commit()
def copy_unknown(contract,mysql):
	res=contract['unknown'].find({})
	cur=mysql.cursor()
	tpl="insert into exchange_unknown (`symbol`, `name`, `order_min`, `precision`, `company_id`, `exchange_id`) VALUES (%s,%s,%s,%s,%s,%s)"
	for row in res:
		cur.execute(tpl,(row['symbol'],row['name'],row['trade_min'],row['precision'],1,exchanges[row['exchange']]))
	cur.close()
	conn.commit()
def copy_cbd(contract,mysql):
	res=contract['CBD'].find({})
	cur=mysql.cursor()
	tpl="insert into exchange_cbd (`symbol`, `name`, `order_min`, `precision`, `company_id`, `exchange_id`) VALUES (%s,%s,%s,%s,%s,%s)"
	i=0
	for row in res:
		#i+=1
		#print(row)
		cur.execute(tpl,(row['symbol'],row['name'],row['trade_min'],row['precision'],1,exchanges[row['exchange']]))
	
	cur.close()
	conn.commit()
def copy_etf(contract,mysql):
	res=contract['ETF'].find({})
	cur=mysql.cursor()
	tpl="insert into exchange_etf (`symbol`, `name`, `order_min`, `precision`, `exchange_id`) VALUES (%s,%s,%s,%s,%s)"
	i=0
	for row in res:
		cur.execute(tpl,(row['symbol'],row['name'],row['trade_min'],row['precision'],exchanges[row['exchange']]))
	
	cur.close()
	conn.commit()
def copy_ind(contract,mysql):
	res=contract['IND'].find({})
	cur=mysql.cursor()
	tpl="insert into exchange_ind (`symbol`, `name`, `precision`, `exchange_id`) VALUES (%s,%s,%s,%s)"
	i=0
	for row in res:
		cur.execute(tpl,(row['symbol'],row['name'],row['precision'],exchanges[row['exchange']]))
	
	cur.close()
	conn.commit()
if __name__ == "__main__":
	print("start import.")
	conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='y',
        passwd='Abc123...',
        db ='sa_data',
        )
	myc = pymongo.MongoClient(remote)
	copy_unknown(myc['contract'],conn)

	
	conn.close()


	
	

